using System;
using System.Diagnostics;
using System.IO.Ports;
using Unme.Common;

namespace Modbus.IO
{
	/// <summary>
	/// Concrete Implementor - http://en.wikipedia.org/wiki/Bridge_Pattern
	/// </summary>
	internal class SerialPortAdapter : IStreamResource
	{
		private SerialPort _serialPort;

		public SerialPortAdapter(SerialPort serialPort)
		{
			Debug.Assert(serialPort != null, "Argument serialPort cannot be null.");

			_serialPort = serialPort;
			_serialPort.NewLine = Modbus.NewLine;
		}

		public int InfiniteTimeout
		{
			get { return SerialPort.InfiniteTimeout; }
		}

		public int ReadTimeout
		{
			get { return _serialPort.ReadTimeout; }
			set { _serialPort.ReadTimeout = value; }
		}

		public int WriteTimeout
		{
			get { return _serialPort.WriteTimeout; }
			set { _serialPort.WriteTimeout = value; }
		}

		public void DiscardInBuffer()
		{
			_serialPort.DiscardInBuffer();
		}

		public int Read(byte[] buffer, int offset, int count)
		{
			return _serialPort.Read(buffer, offset, count);
		}

		public void Write(byte[] buffer, int offset, int count)
		{
			_serialPort.Write(buffer, offset, count);
		}

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (disposing)
				DisposableUtility.Dispose(ref _serialPort);
		}
	}
}
